﻿
namespace HSS.Xaml.UI.Data
{
	#region Using Directives
	using System;
	using System.Windows.Data;
	using System.Windows.Media;
	using HSS.Xaml.UI.Media;
	#endregion

	#region KnownColorValueConverter
	/// <summary>
	/// A value converter of the <see cref="KnownColor"/> type.
	/// </summary>
	public class KnownColorValueConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Convert the value to various formats.
		/// </summary>
		/// <param name="value">The object to convert.</param>
		/// <param name="targetType">The Type to convert to.</param>
		/// <param name="parameter">An optional parameter (not used).</param>
		/// <param name="culture">The CultureInfo (not used).</param>
		/// <returns>The converted object if conversion was performed; otherwise the original <paramref name="value"/>.</returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (targetType == typeof(string))
			{
				if (value is KnownColor)
				{
					KnownColor kc = (KnownColor)value;
					return kc.ToString();
				}

				if (value is Color)
					return ((Color)value).ToString();
			}

			if (targetType == typeof(Color))
			{
				if (value is KnownColor)
				{
					KnownColor kc = (KnownColor)value;
					if (kc == KnownColor.UnknownColor)
						return (Color)(KnownColor)value;
					else
						return (Color)(KnownColor)value;
				}
			}

			if (targetType == typeof(Brush))
			{
				if (value is KnownColor)
				{
					Color color = (Color)(KnownColor)value;
					return new SolidColorBrush(color);
				}

				if (value is Color)
					return new SolidColorBrush((Color)value);
			}

			return value;
		}
		/// <summary>
		/// NotImplementedException
		/// </summary>
		/// <param name="value">Not used.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns>NotImplementedException</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
	#endregion
}